package com.wkck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wkck.VO.DeliverBackendVo;
import com.wkck.VO.PageResult;
import com.wkck.domain.DeliveredLibraryHistory;
import com.wkck.domain.Delivered_Library;
import com.wkck.dto.AuditParamsDto;
import com.wkck.dto.BackendResumeAuditConditionsDto;
import com.wkck.exception.BaseException;
import com.wkck.mapper.DeliveredLibraryHistoryMapper;
import com.wkck.mapper.DeliveredLibraryMapper;
import com.wkck.mapper.ResumeDeliverMapper;
import com.wkck.service.ResumeDeliverService;
import com.wkck.utils.DateTimeGeneration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Author: CK
 * @Date: 2024/03/28/22:50
 * @Description:
 */
@Service
@Slf4j
public class ResumeDeliverServiceImpl implements ResumeDeliverService {

    @Autowired
    private ResumeDeliverMapper deliverMapper;

    @Autowired
    private DeliveredLibraryMapper deliveredLibraryMapper;

    @Autowired
    private DeliveredLibraryHistoryMapper historyMapper;

    /***
     * @Author: CK
     * @Date: 2024/3/28
     * @Param: [dto]
     * @Description: 查询所有需要审核信息
     */
    @Override
    public PageResult getAllResumeAudit(BackendResumeAuditConditionsDto dto) {

        PageHelper.startPage(dto.getCurrentPage(),dto.getSize());

        Page<DeliverBackendVo> result = deliverMapper.getAllResumeAudit(dto);

        return PageResult.builder()
                .records(result.getResult())
                .total(result.getTotal())
                .build();
    }

    /***
     * @Author: CK
     * @Date: 2024/3/29
     * @Param: [AuditParamsDto]
     * @Description: 审核投递简历
     */
    @Override
    @Transactional
    public Boolean auditUserDeliverResume(AuditParamsDto dto) {
        if (Objects.isNull(dto)) {
            throw new BaseException("条件为空 不允许更改");
        }

        if (dto.getIds().size() == 0 ) {
            throw new BaseException("操作对象为空 不允许更改");
        }

        if (dto.getAudit_status() == null) {
            throw new BaseException("审核状态异常");
        }

        deliveredLibraryMapper.updateUserDeliverAuditStatus(dto);

        String audit = historyMapper.getAudit(dto.getAudit_status());

        for (Long id : dto.getIds()) {
            DeliveredLibraryHistory history = DeliveredLibraryHistory.builder()
                    .remark(dto.getRemark())
                    .state_content(audit)
                    .create_time(DateTimeGeneration.now())
                    .dl_id(id)
                    .build();
            historyMapper.insert(history);
        }
        log.info("审核历史数据已插入完毕 ====> {}", DateTimeGeneration.now());

        return true;
    }

    /***
     * @Author: CK
     * @Date: 2024/3/29
     * @Param: [id]
     * @Description: 检查用户简历是否存在
     */
    @Override
    public Boolean checkUserDeliverResume(Long id) {
        if (id == null) {
            throw new BaseException("投递记录不存在 请重新尝试！");
        }

        Delivered_Library deliveredLibrary = deliveredLibraryMapper.selectById(id);
        if (Objects.isNull(deliveredLibrary)) {
            throw new BaseException("投递记录不存在 请重新尝试！");
        }

        return true;
    }

    @Override
    public Boolean checkBatchUserDeliverResume(List<Long> ids) {
        if (ids.size() == 0) {
            throw new BaseException("操作对象为空 不允许更改");
        }

        LambdaQueryWrapper<Delivered_Library> deliveredLibraryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deliveredLibraryLambdaQueryWrapper.in(Delivered_Library::getId, ids);
        List<Delivered_Library> rows = deliveredLibraryMapper.selectList(deliveredLibraryLambdaQueryWrapper);

        if (rows.size() != ids.size()) {
            throw new BaseException("已有用户更改记录！");
        }
        return true;
    }

    /***
     * @Author: CK
     * @Date: 2024/3/29
     * @Param: [id]
     * @Description: 获取用户的已投递的简历详情
     */
    @Override
    public List<DeliveredLibraryHistory> getUserDeliverInfoById(Long id) {
        if (id == null) {
            throw new BaseException("编号为空 无对应信息!");
        }

        LambdaQueryWrapper<DeliveredLibraryHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(DeliveredLibraryHistory::getDl_id, id)
                .orderByDesc(DeliveredLibraryHistory::getCreate_time);

        return historyMapper.selectList(queryWrapper);
    }
}
